// src/module/feedback/issue/issue.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { IssueEntity } from './entities/issue.entity';
import {ResultData} from "../../../common/utils/result";

@Injectable()
export class IssueService {
    constructor(
        @InjectRepository(IssueEntity)
        private readonly issueEntityRep: Repository<IssueEntity>
    ) {}

    /**
     * 查询所有已加入题库的题目，并按 tag 分组
     */
    async getQuestionBankByTag() {
        const questions = await this.issueEntityRep.find({
            where: {
                inStore: 'Y',
                delFlag: '0',
            },
        });

        // 按 tag 分组
        const groupMap = new Map<string, any>();

        questions.forEach(q => {
            const key = q.tag || '未分类';
            if (!groupMap.has(key)) {
                groupMap.set(key, []);
            }
            groupMap.get(key)?.push(q);
        });

        // 转成数组结构
        const data = Array.from(groupMap.entries()).map(([tag, list]) => ({
            tag,
            list
        }));
        return ResultData.ok(data)
    }
}
